﻿using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.Serialization;

namespace Clothes.Items.Preferences
{
    public partial class ItemsPreferencesPanel : ItemsPreferencesPanelNoVis
    {
        private BindableOject<string> Combine = new BindableOject<string>();
        private ListBoxManager<string> DescManager = new ListBoxManager<string>();
        private int idx = -1;
            

        public ItemsPreferencesPanel()
            : base()
        {
            InitializeComponent();
        }
        public ItemsPreferencesPanel(string appName)
            : base(appName)
        {
            InitializeComponent();
            ButtonText = Properties.Resources.T_Items;
            ButtonImage = Properties.Resources.B_Items;
        }

        protected override void OnAttachData()
        {
            FillMasterSource();
            DescManager.AttachTo(lsbDesc);
        }

        private void FillMasterSource()
        {
            DescManager.AddValue("{Categoria}", "Categoria");
            DescManager.AddValue("{Designer}", "Designer");
            DescManager.AddValue("{Taglia}", "Taglia");
            DescManager.AddValue("{Colore}", "Colore");
            DescManager.AddValue("{Stampa}", "Stampa");
            DescManager.AddValue("{Materiale}", "Materiale");
        }

        protected override void OnBindData()
        {
            BindControl(txtDescription, "DescriptionFormat");
            BindControl(ntbDayDeadline, "DayToDeadline");
        }

        protected override void OnPrepareAuxData()
        {
            //if (!string.IsNullOrEmpty(Combine.Value))
            //{
            //    string[] binding = Combine.Value.Split('\t');
            //    for (int t = 0; t < binding.Length; t++)
            //        mlsInvoice.MoveRight(binding[t]);
            //}
        }

        private void txtDescription_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect = System.Windows.Forms.DragDropEffects.Copy;
        }

        private void lsbDesc_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            idx = lsbDesc.IndexFromPoint(e.X, e.Y);
            //if (e.Button != MouseButtons.Left)
            //    idx = -1; ;

            //if (idx >= 0)
            //    DoDragDrop(new DataObject(DataFormats.Text, DescManager.GetKeyAt(idx)), DragDropEffects.Copy);
        }

        private void txtDescription_DragDrop(object sender, DragEventArgs e)
        {
            Point pt = PointToClient(new Point(e.X, e.Y));
            int pos = txtDescription.GetCharIndexFromPosition(pt);

            txtDescription.Text = txtDescription.Text.Substring(0, pos-1) + " " + e.Data.GetData(DataFormats.Text).ToString() + " " + txtDescription.Text.Substring(pos, txtDescription.Text.Length - pos);
            txtDescription.DataBindings["Text"].WriteValue();
        }

        private void lsbDesc_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (idx >= 0)
                txtDescription.Text = txtDescription.Text + " " + DescManager.GetKeyAt(idx);
            txtDescription.DataBindings["Text"].WriteValue();
        }
    }

    #region ItemsPreferencesPanelNoVis
    public partial class ItemsPreferencesPanelNoVis : ApplicationLibrary.Preferences.GenericPreferencePanel<ItemsPref>
    {
        public ItemsPreferencesPanelNoVis()
        { }

        public ItemsPreferencesPanelNoVis(string appName)
            : base(appName, null)
        {
        }
    } 
    #endregion

    [DataContract]
    public class ItemsPref
    {
        [DataMember]
        public string DescriptionFormat { get; set; }

        [DataMember]
        public int DayToDeadline { get; set; }
    }

}
